package com.fingard.dsp.bank.directbank.epay02;

import cn.com.infosec.util.Base64;
import com.alibaba.fastjson.JSONObject;
import com.fingard.dsp.bank.directConfig.ActSetItem;
import com.fingard.dsp.bank.directbank.bankInterface.IGateway;
import com.fingard.dsp.bank.reqrespobj.gateway.ReqGateway;
import com.fingard.dsp.bank.reqrespobj.unifiedorder.ReqQueryOrder;
import com.fingard.dsp.bank.reqrespobj.unifiedorder.RetAtsQueryOrder;

import java.util.HashMap;
import java.util.Map;

public class EPAY02Gateway extends EPAY02Base implements IGateway {
    @Override
    public HashMap<String, String> GenGwMap(ReqGateway p_reqGateway) throws Exception {
        HashMap<String, String> retMap = new HashMap<String, String>();

        /*try {
            String ownAct = p_reqGateway.reqHashMap.get("OurBankAccount");
            ActSetItem actItem = getBankConfig().getActSetItem(ownAct, "");
            if (actItem == null) {
                retMap.put("RespCode", "0");
                retMap.put("RespInfo", "未找到账号[" + ownAct + "]商户配置信息");
                retMap.put("Result", "");
            }
            Map<String, Object> bizContent = new HashMap<String, Object>();
            bizContent.put("PLATCODE", p_reqGateway.reqHashMap.get("PLATCODE"));
            String key = RandomStringUtils.randomAlphanumeric(16);
            String loginCode = encode(key, actItem.merchantId, actItem.appKey);
            bizContent.put("LOGINCODE", loginCode);
            bizContent.put("AMOUNT", p_reqGateway.reqHashMap.get("AMOUNT"));
            bizContent.put("SYNURL", bankFront.getUrl("SYNURL"));
            bizContent.put("ASYNURL", bankFront.getUrl("ASYNURL"));
            bizContent.put("ORDERID", p_reqGateway.reqHashMap.get("ORDERID"));
            bizContent.put("PERENTFLAG", "");
            bizContent.put("COMMENT1", p_reqGateway.reqHashMap.get("COMMENT1"));
            bizContent.put("COMMENT2", p_reqGateway.reqHashMap.get("COMMENT2"));

            Map<String, Object> params = convertMapToMap(bizContent);
            params.remove("PERENTFLAG");
            String signData = convertMapToStr(params);
            WriteBankLogLn("待签名数据：" + signData);
            String signature = sign(signData, "SHA1withRSA", actItem);
            bizContent.put("SIGNSTR", signature);
            String aesKey = rsaEncode(key, actItem);
            bizContent.put("AESKEY", aesKey);

            StringBuilder strResp = new StringBuilder();
            String tmpByteLn = "\r\n";
            strResp.append("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">").append(tmpByteLn);
            strResp.append("<HTML>").append(tmpByteLn);
            strResp.append(" <HEAD><TITLE>sender</TITLE></HEAD>").append(tmpByteLn);
            strResp.append(" <BODY>").append(tmpByteLn);
            strResp.append("<form name=\"submitForm\" action=\"" + bankFront.getUrl("recharge") + "\" method=\"post\">").append(tmpByteLn);
            Iterator<String> it = bizContent.keySet().iterator();
            while (it.hasNext()) {
                String keys = it.next();
                strResp.append("<input type=\"text\" name=\"" + keys + "\" value=\"" + bizContent.get(key) + "\"/>").append(tmpByteLn);
            }
            strResp.append("</form>").append(tmpByteLn);
            strResp.append("<script>window.document.submitForm.submit();</script> ").append(tmpByteLn);
            strResp.append(" </BODY>").append(tmpByteLn);
            strResp.append("</HTML>");
            String result = strResp.toString();
            WriteBankLogStep3(result);

            if (result.length() > 0) {
                retMap.put("RespCode", "0");
                retMap.put("RespInfo", "签名成功");
                retMap.put("Result", result);
            } else {
                retMap.put("RespCode", "-1");
                retMap.put("RespInfo", "签名失败");
            }
        } catch (Exception ex) {
            ex.printStackTrace();
            retMap.put("RespCode", "-1");
            retMap.put("RespInfo", ex.getMessage());
            WriteBankLogLn(ex);
        }*/
        return retMap;
    }

    @Override
    public RetAtsQueryOrder queryGWPay(ReqQueryOrder p_reqGateway) throws Exception {
        RetAtsQueryOrder retAts = new RetAtsQueryOrder();
        retAts.rdSeq = p_reqGateway.rdSeq;

        try {
            ActSetItem actItem = getBankConfig().getActSetItem(p_reqGateway.ownAct, "");
            if (actItem == null) {
                retAts.respCode = "0";
                retAts.respInfo = "未找到账号[" + p_reqGateway.ownAct + "]商户配置信息";
                return retAts;
            }
            JSONObject bizContent = new JSONObject();
            bizContent.put("MERCHATCODE", actItem.merchantId);
            bizContent.put("PLATCODE", p_reqGateway.appId);
            bizContent.put("ORDERID", p_reqGateway.outTransID);
            bizContent.put("CERT", Base64.encode(actItem.oppCertFilePath.getBytes(getCharset())));

            Map<String, Object> params = convertMapToMap(bizContent);
            params.remove("PERENTFLAG");
            String signData = convertMapToStr(params);
            WriteBankLogLn("待签名数据：" + signData);
            String signature = sign(signData, "SHA256withRSA", actItem);
            bizContent.put("SIGNSTR", signature);

            String tmpReqMsg = bizContent.toString();
            WriteBankLogStep2(tmpReqMsg);
            String[] tmpStrRet = sendToBank(tmpReqMsg, bankFront.getUrl("queryGWPay"));

            if (tmpStrRet[0].length() == 0) {
                WriteBankLogStep3(tmpStrRet[1]);
                JSONObject resultObj = JSONObject.parseObject(tmpStrRet[1]);
                String responseCode = resultObj.getString("querycode");
                if ("000000".equals(responseCode)) {
                    JSONObject order = resultObj.getJSONObject("order");
                    retAts.outTransID = order.getString("orderid");
                    retAts.transAmt = order.getString("orderamount");
                    retAts.transState = order.getString("orderstatus");
                    retAts.payInfoCode = order.getString("orderstatus");
                } else {
                    retAts.transState = responseCode;
                    retAts.payInfoCode = responseCode;
                }
                retAts.respCode = "0";
                retAts.respInfo = resultObj.getString("querymsg");
            } else {
                retAts.respCode = "-1";
                retAts.respInfo = "银行响应为空,请联系银行处理!";
            }
        } catch (Exception ex) {
            ex.printStackTrace();
            retAts.respCode = "-1";
            retAts.respInfo = ex.getMessage();
            WriteBankLogLn(ex);
        }
        return retAts;
    }
}
